home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / dflat8.zip / DECOMP.C < prev    next >
Text File  |  1991-09-30  |  3KB  |  126 lines

  1. /* ------------------- decomp.c -------------------- */
  2.  
  3. /*
  4.  * Decompress the application.HLP file
  5.  * or load the application.TXT file if the .HLP file
  6.  * does not exist
  7.  */
  8.  
  9. #include "dflat.h"
  10. #include "htree.h"
  11.  
  12. static int in8;
  13. static int ct8 = 8;
  14.  
  15. static FILE *fi;
  16. static BYTECOUNTER bytectr;
  17.  
  18. static int LoadingASCII;
  19.  
  20. FILE *OpenHelpFile(void)
  21. {
  22.     unsigned char c;
  23.     char *cp;
  24.     int freqctr;
  25.     extern char **Argv;
  26.     char helpname[65];
  27.  
  28.     strcpy(helpname, Argv[0]);
  29.     cp = strrchr(helpname, '\\');
  30.     if (cp == NULL)
  31.         cp = helpname;
  32.     else 
  33.         cp++;
  34.     strcpy(cp, DFlatApplication);
  35.     strcat(cp, ".HLP");
  36.  
  37.     if ((fi = fopen(helpname, "rb")) == NULL)    {
  38.         strcpy(cp, DFlatApplication);
  39.         strcat(cp, ".TXT");
  40.         if ((fi = fopen(helpname, "rt")) == NULL)
  41.             return NULL;
  42.         LoadingASCII = TRUE;
  43.     }
  44.  
  45.     if (!LoadingASCII && ht == NULL)    {
  46.         if ((ht = calloc(256, sizeof(struct htree))) != NULL)    {
  47.             /* ----- read the byte count ------ */
  48.             fread(&bytectr, sizeof bytectr, 1, fi);
  49.             /* ----- read the frequency count ------ */
  50.             fread(&freqctr, sizeof freqctr, 1, fi);
  51.             /* -------- read the characters ---------- */
  52.             while (freqctr--)   {
  53.                 fread(&c, sizeof(char), 1, fi);
  54.                 ht[c].ch = c;
  55.                 fread(&ht[c].cnt, sizeof(BYTECOUNTER), 1, fi);
  56.             }
  57.             /* ---- build the huffman tree ----- */
  58.             buildtree();
  59.         }
  60.     }
  61.     return fi;
  62. }
  63.  
  64. void *GetHelpLine(char *line)
  65. {
  66.     int h;
  67.     if (LoadingASCII)
  68.         return fgets(line, 160, fi);
  69.     *line = '\0';
  70.     while (TRUE)    {
  71.         /* ----- decompress a line from the file ------ */
  72.         h = root;
  73.         /* ----- first get a character ----- */
  74.         while (ht[h].right != -1)    {
  75.             if (ct8 == 8)   {
  76.                 if ((in8 = fgetc(fi)) == EOF)    {
  77.                     *line = '\0';
  78.                     return NULL;
  79.                 }
  80.                 ct8 = 0;
  81.             }
  82.             if (in8 & 0x80)
  83.                 h = ht[h].left;
  84.             else
  85.                 h = ht[h].right;
  86.             in8 <<= 1;
  87.             ct8++;
  88.         }
  89.         if ((*line = ht[h].ch) == '\r')
  90.             continue;
  91.         if (*line == '\n')
  92.             break;
  93.         line++;
  94.     }
  95.     *++line = '\0';
  96.     return line;
  97. }
  98.  
  99. void HelpFilePosition(long *offset, int *bit)
  100. {
  101.     *offset = ftell(fi);
  102.     if (LoadingASCII)
  103.         *bit = 0;
  104.     else    {
  105.         if (ct8 < 8)
  106.             --*offset;
  107.         *bit = ct8;
  108.     }
  109. }
  110.  
  111. void SeekHelpLine(long offset, int bit)
  112. {
  113.     int i;
  114.     fseek(fi, offset, 0);
  115.     if (!LoadingASCII)    {
  116.         ct8 = bit;
  117.         if (ct8 < 8)    {
  118.             in8 = fgetc(fi);
  119.             for (i = 0; i < bit; i++)
  120.                 in8 <<= 1;
  121.         }
  122.     }
  123. }
  124.  
  125.  
  126.